package com.study.sfd.person.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.sfd.person.dao.DoctorPatientRelationDao;
import com.study.sfd.person.dao.PatientDao;
import com.study.sfd.person.entity.DoctorPatientRelationEntity;
import com.study.common.entity.PatientEntity;
import com.study.sfd.person.service.PatientService;
import com.study.sfd.person.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("patientService")
public class PatientServiceImpl extends ServiceImpl<PatientDao, PatientEntity> implements PatientService {

    @Autowired
    DoctorPatientRelationDao doctorPatientRelationDao;

    @Override
    public List<PatientEntity> listPatients()
    {
        Map<String, Object> map = ThreadLocalUtil.get();
        Object id = map.get("doctorId");
        return listByDoctorId((int) id);
    }

    public List<PatientEntity> listByDoctorId(int doctorId)
    {
        List<Integer> patientIds = doctorPatientRelationDao.selectList(
                        new LambdaQueryWrapper<DoctorPatientRelationEntity>()
                                .eq(DoctorPatientRelationEntity::getDoctorId, doctorId)
                ).stream()
                .map(DoctorPatientRelationEntity::getPatientId)
                .collect(Collectors.toList());
        return baseMapper.selectBatchIds(patientIds);
    }
}